﻿using System;
using System.Web.Mvc;
using NUnit.Framework;

namespace RESTController
{
    public class MvcAssert
    {
        public static TModel HasModelOfType<TModel>(ViewResult viewResult)
        {
            Assert.IsInstanceOf<TModel>(viewResult.ViewData.Model);
            return (TModel)(viewResult.ViewData.Model);
        }

        public static TActionResult IsActionResultOfType<TActionResult>(ActionResult actionResult) where TActionResult : ActionResult
        {
            try
            {
                return (TActionResult)actionResult;
            }
            catch (InvalidCastException)
            {
                throw new AssertionException(string.Format("Expected an ActionResult of type {0}, but instead found a {1}.", typeof(TActionResult).Name, actionResult.GetType()));                
            }
            throw new Exception("Unknown error in assertion.");
        }
    }
}
